// pages/new_inter/hotel_order/hotel_order.js
const app = getApp();
Page({  
  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    array: ['是', '否'],
    index: 0,
    isShows: false,
    phone:"",
    gety_but:false,
    time_out:'block',
    is_send:1
  },
  // 时间选择器
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var date = e.detail.value;
    this.setData({
      date: e.detail.value
    })
    this.getEndDate(date, this.data.info.days);
  },
  getEndDate:function(value, days){
    var d = new Date(value);
    d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
    var s = d.getFullYear() + "-" + this.Appendzero(d.getMonth() + 1) + "-" + d.getDate();
    this.setData({
      booking_end: s
    })
  },
  Appendzero:function(obj){
    if(obj < 10) return "0" + "" + obj;
    else return obj;
  },
  // 是否持卡人使用选择器
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    });
    if (e.detail.value==0){
      this.setData({
        isShows: false
      })
    }else{
      this.setData({
        isShows: true
      })
    }

    // console.log(this.data.isShows);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

    if (options.OrderCode){
      wx.request({
        url: app.globalData.apiUrl + 'Hotel/infoData',
        data: {
          // opt: 'getBanner',
          order_code: options.OrderCode
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (res.data != null) {
            if (res.data.OrderState == '未预订') {
              that.setData({
                info: res.data,
                phone: res.data.Mobile
              })
              //获得目的地
              // $('[name=place]').val(data.HotelTitle);
              // //入驻时间范围
              // $('[name=booking_start]').removeAttr('readonly');
              // $('[name=booking_start]').attr('days', data.days);
              // $('[name=booking_start]').each(function () {
              //   layui.laydate.render({
              //     elem: this
              //     , min: data.start_date
              //     , max: data.end_date
              //     , done: function (value, date) {
              //       getEndDate(value, $('[name=booking_start]').attr('days'))
              //     }
              //   });
              // })
            } else {
              wx.showToast({
                title: '旅居卡不能使用',
                icon: 'none'
              })
            }
          }
        }
      })
    }
    that.yuyue_xieyi();
  },
  //失去焦点事件
  get_card_info:function(e){
    var that=this;
    var code=e.detail.value;
    if(code){
      wx.request({
        url: app.globalData.apiUrl + 'Hotel/infoData',
        data: {
          // opt: 'getBanner',
          order_code:code
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (res.data != null) {
            
            if (res.data.OrderState == '未预订') {
              that.setData({
                info: res.data,
                phone: res.data.Mobile
              })
              //获得目的地
              // $('[name=place]').val(data.HotelTitle);
              // //入驻时间范围
              // $('[name=booking_start]').removeAttr('readonly');
              // $('[name=booking_start]').attr('days', data.days);
              // $('[name=booking_start]').each(function () {
              //   layui.laydate.render({
              //     elem: this
              //     , min: data.start_date
              //     , max: data.end_date
              //     , done: function (value, date) {
              //       getEndDate(value, $('[name=booking_start]').attr('days'))
              //     }
              //   });
              // })
            } else {
              wx.showToast({
                title: '旅居卡不能使用',
                icon:'none'
              })
            }
          }
        }
      })
    }
  },
  blurPhone: function (e) {
    console.log(e)
    var phone = e.detail.value;
    let that = this
    if (!(/^1[34578]\d{9}$/.test(phone))) {

      this.setData({
        ajxtrue: false
      })
      if (phone.length > 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true,
        phone: phone,
      })
      
    }
    console.log('验证成功', that.data.ajxtrue)
  },
  //获取验证码
  get_yanzheng:function(){
    var that=this;
    if (that.data.phone==""||that.data.ajxtrue==false){
      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        duration: 2000
      })
      return ;
    }else{
      wx.request({
        url: app.globalData.apiUrl + 'User/getYzm',
        data: {
          // opt: 'getBanner',
          phone: that.data.phone
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)

          if (res.data.status == 1) {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
              that.setData({
                yanzhengma: res.data.code,
                gety_but:true,
                is_send:1
              })
            var flag = true;
            var n = 5;
            var current = 5;
            that.setData({
              wait_time: n,
              time_out: 'block',
            })
            var timer = null;
            timer = setInterval(function () {
              n--;
              that.setData({
                wait_time: n,
              })
              if (n <= 0) {
                clearInterval(timer);
                n = current;
                var nowflag = true;
                var flag = false;
                that.setData({
                  is_send:0,
                  gety_but:false
                })
              }
            }, 1000);
              
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          }
      })
    }
  },
  //提交
  formSubmit: function (e) {
    console.log(e)

    var that = this;
    var formData = e.detail.value;
    console.log(that.data.yanzhengma)
    if (Number(formData['mobile_code']) !== that.data.yanzhengma){
      wx.showToast({
        title: '验证码不正确',
        icon: 'none'
      })
      return;
    }
    // if (formData['Code'] == "" || formData['PassWord_2'] == "" || formData['yanzhengma'] == "" || formData['RefereeCode'] == "") {
    //   wx.showToast({
    //     title: '必填',
    //     icon: 'none'
    //   })
    //   return;
    // }
    // that.setData({
    //   button: 'false'
    // })
    // console.log(that.data.button)
    // return ;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/bookingDo',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            content: '您已提交申请，24小时之内等待答复',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../../personal/lvjuka/lvjuka',
                })
              } else if (res.cancel) {
                wx.navigateTo({
                  url: '../../personal/lvjuka/lvjuka',
                })
              }
            }
          })
      
          // wx.showToast({
          //   title: res.data.message,
          //   icon: 'none',

          //   success: function () {
          //     setTimeout(function () {
          //       wx.navigateTo({
          //         url: '../../personal/lvjuka/lvjuka',
          //       })
          //     }, 2000);
          //   }
          // })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        // that.setData({
        //   button: 'true'
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady: function() {
    this.alertview = this.selectComponent('#alertview')
  },
  // 按钮点击事件
  clickMe: function() {
    this.alertview.showAlertView();
  },
  // 确定事件
  confirmEvent: function() {
    this.alertview.hideAlertView();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },
  yuyue_xieyi: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/kanfang_xieyi',
      data: {
      },
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data !== null) {
          var desc = that.delHtmlTag(res.data['content'])
          res.data['content'] = desc;
          that.setData({
            info: res.data

          })
        }

      }
    })
  },
  delHtmlTag: function (msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    // msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    // msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  index: function () {
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },
})